function showModal() {
    $('.ui.basic.modal').modal('setting', 'closable', false).modal('show');
}
function showItemsEntry(itemId, itemName, noItems, itemCost, ItemsPrice) {

    $('#itemsInfoTable tr:last').after("<tr><td class='itemId'>" + itemId + "<input type='hidden' name='itemId' value='" + itemId + "' /></td>" +
            "<td class='bItemName'>" + itemName + "<input type='hidden' name='itemName' value='" + itemName + "'/></td>" +
            "<td class='nItems'>" + noItems + "<input type='hidden'  name='noItems' value='" + noItems + "'/></td>" +
            "<td class='sItemCost'>" + itemCost + "<input type='hidden'  name='itemCost' value='" + itemCost + "'/></td>" +
            "<td class='totalItemsCost'>" + ItemsPrice + "<input type='hidden'  name='ItemsPrice' id='itemCost' value='" + ItemsPrice + "'/></td>" +
            "<td><input type='button' value='Remove' class='ui blue button actionButton' onclick='removeSelectedRow(this);'></td>" + "</tr>");

    $('#itemId').val('');
    $('#itemName').val('');
    $('#noItems').val('');
    $('#itemCost').val('');
    $('#ItemsPrice').val('');
}

function removeSelectedRow(obj) {
    var totalCost = parseInt($('#total').val());
    var cellArray = obj.closest('tr').cells;
    var selectedTotal = parseInt(cellArray[4].innerText);
    $('#total').val(totalCost - selectedTotal);
    obj.closest('tr').remove();
}

function getKeyDetails() {
    var options = {};
    options.key = $("#itemId").val();
    if (options.key.length == 0 || options.key == ' ') {
        return;
    }
    options.url = "/ShopInventory/getinfo?info=itemId";
    options.type = "GET";
    options.data = {"criteria": options.key};
    options.success = function(data) {
        if (data.length != 0) {
            $("#itemDiv").addClass("ui red error");
            $(".ui.red.message").show().html(data);
        }
    },
            options.error = function(error) {
        console.log("Error Occured...." + error);
    };
    $.ajax(options);
}

function getCustomerNames(keyId, phone) {
    var options = {};
    options.key = keyId;
    if (options.key.length == 0 || options.key == ' ') {
        phone.val('');
        return;
    }
    options.url = "/ShopInventory/getinfo?info=names";
    options.type = "GET";
    options.data = {"criteria": options.key};
    options.success = function(data) {
        var names = JSON.parse(data);
        if (names.length != 0) {
            $("#custNames").empty();
            for (var i = 0; i < names.length; i++)
            {
                $("#custNames").append("<option value='" +
                        names[i].customer_Name + "'></option>");
                if (phone != null) {
                    phone.val(names[i].customer_Phone);
                }
            }
        }
    },
            options.error = function(error) {
        console.log("Error Occured...." + error);
    };
    $.ajax(options);
}

function getItemDescription(params) {
    console.log("Object Info:-" + params.date);
    var options = {};
    options.url = "/ShopInventory/getinfo?info=itemDescription";
    options.type = "GET";
    options.data = {"custName": params.customerName, "date": params.date, "mobile": params.mobile};
    options.success = function(data) {
        console.log(data);
        var soldItems = JSON.parse(data);
        for (var i = 0; i < soldItems.length; i++)
        {
            console.log("Item Id:-" + soldItems[i].item_Id);
            console.log("Item Quantity:-" + soldItems[i].quantity);
            console.log("Item Price:-" + soldItems[i].rate);
            console.log("Total Price:-" + soldItems[i].price);
            alert("Item Id:-" + soldItems[i].item_Id + "Item Quantity:-" + soldItems[i].quantity + "Item Price:-" + soldItems[i].rate + "Total Price:-" + soldItems[i].price);
        }
    },
            options.error = function(error) {
        console.log("Error Occured...." + error);
    };
    $.ajax(options);
}


function calculateTotal(itemInput1, itemInput2) {
    var itemCost = parseInt(itemInput1.val());
    var noItem = parseInt(itemInput2.val());
    var totalPrice = itemCost * noItem;
    $('#ItemsPrice').val(totalPrice);

}
